package cn.mw.cmdb.param;

import cn.mw.cmdb.entity.ItemDropInfo;
import com.alibaba.nacos.common.utils.CollectionUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 监控历史数据
 */
@Data
public class QueryItemHistoryParam extends QueryMonitorParam {
    @ApiModelProperty("开始时间")
    private Long startDate;
    @ApiModelProperty("结束时间")
    private Long endDate;
    @ApiModelProperty("itemId集合")
    private List<ItemDropInfo> itemInfos;
    @ApiModelProperty("itemId集合")
    private List<String> itemIds;
    @ApiModelProperty("数据类型0：数字浮点；1：字符；2：日志；3：数字无符号；4：文字")
    private Integer valueType;
    private int dateType;


    public Map<String, ItemDropInfo> getItemInfoByItemId() {
        Map<String, ItemDropInfo> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(itemInfos)) {
            map = itemInfos.stream().peek(s->s.itemIdsConvert()).filter(s->CollectionUtils.isNotEmpty(s.getItemids())).flatMap(bean -> bean.getItemids().stream().map(str -> new AbstractMap.SimpleEntry<>(str, bean))).collect(Collectors.groupingBy(AbstractMap.SimpleEntry::getKey, Collectors.mapping(AbstractMap.SimpleEntry::getValue, Collectors.collectingAndThen(Collectors.toList(), list -> list.get(0)))));
        }
        return map;
    }

    public Map<String, ItemDropInfo> getItemInfoByItemName() {
        Map<String, ItemDropInfo> map = new HashMap<>();
        if (CollectionUtils.isNotEmpty(itemInfos)) {
            map = itemInfos.stream().collect(Collectors.toMap(s -> s.getName(), s -> s, (o1, o2) -> o1));
        }
        return map;
    }
}
